package com.rxmvp.http;

import android.net.ParseException;
import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.loper7.base.utils.Logger;
import com.rxmvp.http.exception.ApiException;
import com.rxmvp.http.exception.ErrorType;
import com.rxmvp.http.exception.ServerException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * 异常 的 提示转化
 */
public class ExceptionEngine {
    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof HttpException) {             //HTTP错误
            HttpException httpException = (HttpException) e;
            ex = new ApiException(e, ErrorType.HTTP_ERROR);
            String[] values = e.getMessage().split(" ");
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex = new ApiException(e, ErrorType.LOGIN_OUT);
                    ex.message = values.length == 0 ? e.getMessage() : values[values.length - 1];
                    break;
                case FORBIDDEN:
                    ex = new ApiException(e, ErrorType.OPERATE_ERROR);
                    ex.message = values.length == 0 ? e.getMessage() : values[values.length - 1];
                    break;
                case NOT_FOUND:
                    ex.message = "服务器异常，请稍后再试";
                    break;
                case REQUEST_TIMEOUT:
                    ex.message = "请求超时";
                    break;
                case GATEWAY_TIMEOUT:
                    ex.message = "作为网关或者代理工作的服务器尝试执行请求时，未能及时从上游服务器（URI标识出的服务器，例如HTTP、FTP、LDAP）或者辅助服务器（例如DNS）收到响应";
                    break;
                case INTERNAL_SERVER_ERROR:
                    ex.message = "服务器遇到了一个未曾预料的状况，导致了它无法完成对请求的处理";
                    break;
                case BAD_GATEWAY:
                    ex.message = "作为网关或者代理工作的服务器尝试执行请求时，从上游服务器接收到无效的响应";
                    break;
                case SERVICE_UNAVAILABLE:
                    ex.message = "由于临时的服务器维护或者过载，服务器当前无法处理请求";
                    break;
                default:
                    ex.message = "网络错误";  //其它均视为网络错误
                    break;
            }
            return ex;
        } else if (e instanceof ServerException) {    //服务器返回的错误
            ServerException resultException = (ServerException) e;
            ex = new ApiException(resultException, resultException.code);
            ex.message = resultException.message;
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ApiException(e, ErrorType.PARSE_ERROR);
            ex.message = "解析错误";            //均视为解析错误
            return ex;
        } else if (e instanceof ConnectException || e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException) {
            ex = new ApiException(e, ErrorType.NETWORD_ERROR);
            ex.message = "网络请求超时，请重试";  //均视为网络错误
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex = new ApiException(e, ErrorType.NETWORD_ERROR);
            ex.message = "无网络连接，请检查网络设置";  //1.缺少相应的网络权限,2.服务器已关闭或出现域名解析问题,3.网络服务、数据服务是否正常开启
            return ex;
        } else {
            ex = new ApiException(e, ErrorType.UNKNOWN);
            ex.message = "无网络连接，请检查网络设置";          //未知错误
            return ex;
        }
    }

}
